package me.zhengjie.pm.projectstaff.service.impl;

import me.zhengjie.pm.projectstaff.domain.ProjectStaff;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.pm.projectstaff.repository.ProjectStaffRepository;
import me.zhengjie.pm.projectstaff.service.ProjectStaffService;
import me.zhengjie.pm.projectstaff.service.dto.ProjectStaffDto;
import me.zhengjie.pm.projectstaff.service.dto.ProjectStaffQueryCriteria;
import me.zhengjie.pm.projectstaff.service.mapper.ProjectStaffMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @author ZGZ
* @date 2020-04-14
*/
@Service
//@CacheConfig(cacheNames = "projectStaff")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class ProjectStaffServiceImpl implements ProjectStaffService {

    private final ProjectStaffRepository projectStaffRepository;

    private final ProjectStaffMapper projectStaffMapper;

    public ProjectStaffServiceImpl(ProjectStaffRepository projectStaffRepository, ProjectStaffMapper projectStaffMapper) {
        this.projectStaffRepository = projectStaffRepository;
        this.projectStaffMapper = projectStaffMapper;
    }

    @Override
    //@Cacheable
    public Map<String,Object> queryAll(ProjectStaffQueryCriteria criteria, Pageable pageable){
        Page<ProjectStaff> page = projectStaffRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(projectStaffMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<ProjectStaffDto> queryAll(ProjectStaffQueryCriteria criteria){
        return projectStaffMapper.toDto(projectStaffRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public ProjectStaffDto findById(Integer id) {
        ProjectStaff projectStaff = projectStaffRepository.findById(id).orElseGet(ProjectStaff::new);
        ValidationUtil.isNull(projectStaff.getId(),"ProjectStaff","id",id);
        return projectStaffMapper.toDto(projectStaff);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public ProjectStaffDto create(ProjectStaff resources) {
        return projectStaffMapper.toDto(projectStaffRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(ProjectStaff resources) {
        ProjectStaff projectStaff = projectStaffRepository.findById(resources.getId()).orElseGet(ProjectStaff::new);
        ValidationUtil.isNull( projectStaff.getId(),"ProjectStaff","id",resources.getId());
        projectStaff.copy(resources);
        projectStaffRepository.save(projectStaff);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            projectStaffRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<ProjectStaffDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ProjectStaffDto projectStaff : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("项目", projectStaff.getProjectId());
            map.put("员工", projectStaff.getStaffId());
            map.put("工作类型", projectStaff.getWorkType());
            map.put("工作职位", projectStaff.getWorkPosition());
            map.put("工作内容", projectStaff.getWorkContent());
            map.put("开始时间", projectStaff.getStartTime());
            map.put("结束时间", projectStaff.getEndTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}